<template>
  <div class="searchResult-container">
    <!-- 1.导航页 -->
    <van-nav-bar
      :title="`${$route.query.q}的搜索结果`"
      left-arrow
      @click-left="$router.back()"
      fixed
      placeholder
    />
    <!-- 2.上拉列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" 
      @click="$router.push({path:'/article',query:{id:item.art_id}})"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchList } from './home.js'
export default {
  name: 'searchResult',
  data () {
    return {
      loading: false,
      finished: false,
      list: [],
      page:1
    }
  },
  methods: {
    // 上拉加载更多数据
    async onLoad () {
      // 1.请求数据
    let res = await getSearchList({
      page:this.page,
      q:this.$route.query.q
    })
      console.log(res);
      // 2.数据绑定
      this.list.push(...res.data.data.results)
      // 3.页码自增
      this.page++
      // 3.关闭动画
      this.loading=false
      // 4.判断是否加载完毕(服务器给你一个空数组)
      if (res.data.data.results.length == 0) {
        this.finished =true
      }
    }
  }
}
</script>

<style lang="less">
.searchResult-container {
  .van-nav-bar__content {
    background-color: #008bfa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon {
      color: #fff;
    }
  }
}
</style>
